﻿using AnlianCommon.Common.Domains.ProductionManagement;
using AnlianCommon.Common.Domains.SheepFarmHouse;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;
using SheepCommon.API.BreedingHistoryss;

namespace SheepCommon.API.LambWeanings
{
    public class LambWeaningSearchHander : IRequestHandler<LambWeaningSearchCommand, ResultPage<LambWeaningSearchDto>>
    {
        private readonly IBaseRepository<LambWeaning> _lambWeaningRepository;
        private readonly IBaseRepository<Cottage> _cottageRepository;
        private readonly IBaseRepository<Field> _fieldRepository;
        private readonly IMapper _mapper;
        public LambWeaningSearchHander(IBaseRepository<LambWeaning> lambWeaningRepository, IBaseRepository<Cottage> cottageRepository, IBaseRepository<Field> fieldRepository, IMapper mapper)
        {
            _lambWeaningRepository = lambWeaningRepository;
            _cottageRepository = cottageRepository;
            _fieldRepository = fieldRepository;
            _mapper = mapper;
        }
        /// <summary>
        /// 羊羔断奶显示查询
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ResultPage<LambWeaningSearchDto>> Handle(LambWeaningSearchCommand request, CancellationToken cancellationToken)
        {
            var result = new ResultPage<LambWeaningSearchDto>() { Code = (int)CommonErrorCode.Search, TotalCount = 0, PagedCount = 0 };
            try
            {
                var lambWeanings = await(from a in  await _lambWeaningRepository.GetAll()
                                         join b in await _cottageRepository.GetAll() on a.CottageId equals b.CottageId
                                         join c in await _fieldRepository.GetAll() on a.FieldId equals c.FieldId
                                         select  new LambWeaningSearchDto()
                                         {
                                             LambWeaningId=a.LambWeaningId,
                                             LambEarBugle = a.LambEarBugle,
                                             Sex = a.Sex,
                                             CurrentBuildings = a.CurrentBuildings,
                                             CurrentColumns = a.CurrentColumns,
                                             CottageId = b.CottageId,
                                             CottageName = b.CottageName,
                                             FieldId = c.FieldId,
                                             FieldName = c.FieldName,
                                             WeanedWeight = a.WeanedWeight,
                                             CreatedOwnerId=a.CreatedOwnerId,
                                             CreatedTime=a.CreatedTime,
                                             UpdatedOwnerId=a.UpdatedOwnerId,
                                             UpdatedTime=a.UpdatedTime,
                                         }).ToListAsync();
                if (!string.IsNullOrEmpty(request.LambEarBugle))
                {
                    lambWeanings = lambWeanings.Where(p => p.LambEarBugle == request.LambEarBugle).ToList();
                }
                if (!string.IsNullOrEmpty(request.WeanedWeight))
                {
                    lambWeanings = lambWeanings.Where(p => p.WeanedWeight == request.WeanedWeight).ToList();
                }

                result.TotalCount = lambWeanings.Count;
                result.PagedCount = (int)Math.Ceiling((result.TotalCount * 1.0) / request.PageSize);
                lambWeanings = lambWeanings.OrderByDescending(c => c.LambWeaningId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
                result.Data = lambWeanings;
            }
            catch (Exception ex)
            {
                throw;
            }
            return result;
        }
    }
}
